package com.ruoyi.jiaopei.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.jiaopei.domain.TbStudent;
import com.ruoyi.jiaopei.domain.vo.TbStudentVo;
import com.ruoyi.jiaopei.mapper.TbStudentMapper;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jiaopei.domain.vo.TbUserStudentVo;
import com.ruoyi.jiaopei.domain.bo.TbUserStudentBo;
import com.ruoyi.jiaopei.service.ITbUserStudentService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户学生
 *
 * @author macy
 * @date 2024-06-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/jiaopei/userStudent")
public class TbUserStudentController extends BaseController {

    private final ITbUserStudentService iTbUserStudentService;
    private final TbStudentMapper studentMapper;
    private final ISysUserService sysUserService;
    /**
     * 查询用户绑定学生信息
     */
    @GetMapping("/userByStudentList")
    public R<List<TbStudentVo>> userByStudentList() {

        List<TbStudentVo> voList=new ArrayList<>();

        TbUserStudentBo bo1=new TbUserStudentBo();
        bo1.setUserId(LoginHelper.getUserId());
        List<TbUserStudentVo> userStudentVoList=iTbUserStudentService.queryList(bo1);
        if(null!=userStudentVoList && userStudentVoList.size()>0){
            List<Long> ids = userStudentVoList.stream().map(TbUserStudentVo::getStudentId).collect(Collectors.toList());
            voList=studentMapper.selectVoList(new LambdaQueryWrapper<TbStudent>()
                .in(TbStudent::getId,ids)
            );
            if(null!=voList && voList.size()>0){
                voList.forEach(vo->{
                    vo.setTeacherVo(sysUserService.selectUserById(vo.getTeacherId()));
                });
            }
        }
        return R.ok(voList);
    }



    /**
     * 查询用户学生列表
     */
    @GetMapping("/list")
    public TableDataInfo<TbUserStudentVo> list(TbUserStudentBo bo, PageQuery pageQuery) {
        return iTbUserStudentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户学生列表
     */
    @SaCheckPermission("jiaopei:userStudent:export")
    @Log(title = "用户学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbUserStudentBo bo, HttpServletResponse response) {
        List<TbUserStudentVo> list = iTbUserStudentService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户学生", TbUserStudentVo.class, response);
    }

    /**
     * 获取用户学生详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("jiaopei:userStudent:query")
    @GetMapping("/{userId}")
    public R<TbUserStudentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(iTbUserStudentService.queryById(userId));
    }

    /**
     * 新增用户学生
     */
    @SaCheckPermission("jiaopei:userStudent:add")
    @Log(title = "用户学生", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TbUserStudentBo bo) {
        return toAjax(iTbUserStudentService.insertByBo(bo));
    }

    /**
     * 修改用户学生
     */
    @SaCheckPermission("jiaopei:userStudent:edit")
    @Log(title = "用户学生", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbUserStudentBo bo) {
        return toAjax(iTbUserStudentService.updateByBo(bo));
    }

    /**
     * 删除用户学生
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("jiaopei:userStudent:remove")
    @Log(title = "用户学生", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        return toAjax(iTbUserStudentService.deleteWithValidByIds(Arrays.asList(userIds), true));
    }
}
